﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import flash.events.*;
    import flash.net.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import mx.utils.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.services.*;

    public class LoadDetailsCommand extends Command {

		[Inject]
        public var socialNetworkService:ISocialNetworkService;
		[Inject]
        public var enviromentModel:EnvironmentModel;
		[Inject]
        public var extInterface:ExtInterfaceService;
		[Inject]
        public var friendsModel:FriendsModel;
		[Inject]
        public var userModel:UserModel;
        private var _users:Array;
        private var _infos:Array;

        override public function execute():void{
            this.socialNetworkService.getFriends(this.onFriends);
            this.socialNetworkService.getAppUsers(this.onAppUsers);
        }
        private function onFriends(_arg1:Array):void{
            this.extInterface.callJS("setFriends", [_arg1]);
        }
        private function onAppUsers(_arg1:Array):void{
            this._users = _arg1;
            this.extInterface.callJS("setPlayers", [this._users]);
            var _local2:Number = Number(this.userModel.mySocialID);
            this._users.push(_local2);
            this.addFakeFriend(this._users);
            this.socialNetworkService.getInfo(this._users, ["first_name", "picture"], this.onInfo);
        }
        protected function addFakeFriend(_arg1:Array):void{
            _arg1.push(GameConstants.KARLA_FACEBOOK_ID);
        }
        private function onInfo(_arg1:Array):void{
            this._infos = _arg1;
            var _local2:Object = {ids:this._users.join(",")};
            var _local3:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onResult);
            _local3.addRequest("load_friends", _local2);
            dispatch(_local3);
        }
        private function onResult(_arg1:ResultVO):void{
            if (_arg1.success){
                this.updateFriends(_arg1.data.load_friends);
            };
            this.removeSharedObject();
        }
        private function updateFriends(_arg1:Object):void{
            var _local2:Object;
            var _local5:Object;
            if ((((this.friendsModel.friends.length == 0)) && ((this.userModel.level >= 3)))){
                this.openGiftPopup();
            };
            var _local3:Array = [];
            var _local4:Array = [];
            for each (_local5 in this._infos) {
                _local5.pic_square = ((_local5.pic_square) || (_local5.picture));
                _local5.uid = ((_local5.uid) || (_local5.id));
                _local2 = _arg1[_local5.uid];
                if (_local2){
                    _local5.xp = int(_local2.xp);
                    _local5.level = int(_local2.level);
                    _local5.bird = _local2.bird;
                    _local5.symbol = _local2.symbol;
                    _local5.symbolToHarvest = Boolean(_local2.tree);
                    _local3.push(_local5);
                    _local4.push(_local5.uid);
                };
            };
            this.friendsModel.friends = _local3;
            this.friendsModel.sortFriends();
            dispatch(new Event(CommandConstants.ADD_FRIENDS));
            dispatch(new Event(EventConstants.CHECK_FOR_SURPRISE_PACKAGES));
        }
        protected function openGiftPopup():void{
            var _local1:int = Numbers.randomIntBetween(1, 5);
            if (_local1 == 1){
                dispatch(new InfoEvent(EventConstants.SHOW_GIFTS_POPUP, null, EventConstants.WELCOME_GIFTS_POPUP));
            };
        }
        private function removeSharedObject():void{
            var url:String;
            var api_key:String;
            var sharedObject:SharedObject;
            if (!this.enviromentModel.localVersion){
                url = contextView.stage.loaderInfo.url;
                l(("url: " + url));
                api_key = contextView.stage.loaderInfo.parameters["fb_sig_api_key"];
                try {
                    sharedObject = SharedObject.getLocal((api_key + "_stored_session"));
                    l(("c shared object: " + ObjectUtil.toString(sharedObject.data)));
                    l("c ---CLEAR---");
                    sharedObject.clear();
                    l(("c cleared shared object: " + ObjectUtil.toString(sharedObject.data)));
                } catch(error:Error) {
                    l("f shared object not found");
                };
            };
        }

    }
}//package net.wooga.pets.controllers 
